package edu.gmu.projects.swe795;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class MethodLineCounter {

	String filename;

	public MethodLineCounter(String filename) {
		this.filename = filename;
	}

	public void countLinesPerMethod() {

		try {

			File in = new File(filename);
			BufferedReader reader = new BufferedReader(new FileReader(in));
			boolean skipNextOpenBracket = false;
			int bracketStatusCount = 0;
			String className = null;
			String methodName = null;
			int parameterCount = 0;
			int lineCount = 0;

			try {
				PrintWriter out = new PrintWriter(new FileWriter(filename
						+ ".txt"));

				String text = null;

				try {
					while ((text = reader.readLine()) != null) {
						if (text.matches("^.*\\sclass\\s.*$")
								&& !text.matches("^.*\".*class.*\".*$")) {

							String[] temp = text.split("\\sclass\\s");
							className = temp[temp.length - 1].split("\\s+")[0];
							out.println(className);

							skipNextOpenBracket = true;
						}

						if (text.contains("{")
								&& !text.matches("^.*\".*\\x7B.*\".*$")) {
							if (!skipNextOpenBracket) {

								bracketStatusCount++;

								if (bracketStatusCount == 1) {
									String[] temp = text.split("\\(");

									if (temp.length == 2) {
										String[] beforeParameters = temp[0]
												.split("\\s+");
										String[] parameters = temp[1]
												.split(",");

										for (String parameter : parameters) {
											String cleanedParam = parameter
													.split("\\)")[0];

											if (cleanedParam.length() != 0) {
												parameterCount++;
											}
										}

										methodName = beforeParameters[beforeParameters.length - 1];
									}
								}

							} else {
								skipNextOpenBracket = false;
							}
						}

						if (bracketStatusCount > 0 && !text.matches("^\\s*$")) {
							lineCount++;
						}

						if (text.contains("}")
								&& !text.matches("^.*\".*\\x7D.*\".*$")) {

							bracketStatusCount--;

							if (bracketStatusCount == 0) {

								out.println(methodName + " has " + lineCount
										+ " lines and " + parameterCount
										+ " parameters.");

								lineCount = 0;
								parameterCount = 0;
								methodName = null;
							}
						}
					}

					out.close();
				} catch (IOException e) {
					System.out.println("Error reading from file.");
				}

			} catch (IOException e1) {
				System.out.println("Error writing to file.");
			}

		} catch (FileNotFoundException e) {
			System.out.println("File does not exist.");
		}
	}

	public static void main(String[] args) {

		if (args.length > 0) {

			traversePath(args[0]);

		} else {

			System.out.println("No command line arguments.");

		}

	}

	private static void traversePath(String path) {

		if (path.length() != 0) {
			File dir = new File(path);

			if (dir.isDirectory()) {

				for (File file : dir.listFiles()) {

					if (!file.getName().matches("^\\..*$")
							&& file.getName().matches("^.*[\\.c|\\.java]$")) {
						traversePath(file.getPath());
					}

				}

			} else {

				MethodLineCounter mlc = new MethodLineCounter(path);
				mlc.countLinesPerMethod();

			}
		}

	}

}
